package cn.zjxiliu.lib_login.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import cn.zjxiliu.lib_core.widget.ClearEditText;
import cn.zjxiliu.lib_login.R;

/**
 * Created by jessie on 2018/11/21.
 */

public class AccountInputView extends LinearLayout{

    private View viewLine;
    private ClearEditText etAccount;
    private LinearLayout llAccountInput;

    private InputViewCallbackListener listener;

    public void setListener(InputViewCallbackListener listener) {
        this.listener = listener;
    }

    public AccountInputView(Context context) {
        super(context);
    }

    public AccountInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        llAccountInput = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.input_account_view, null);

        etAccount = (ClearEditText) llAccountInput.findViewById(R.id.etAccount);
        viewLine = llAccountInput.findViewById(R.id.viewLine);

        initListen();
        addView(llAccountInput);
    }

    public AccountInputView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initListen() {
        etAccount.setTextChangeListener(new ClearEditText.OnTextChangeListener() {
            @Override
            public void onFocusChange(boolean hasFocus) {
                if (hasFocus) {
                    viewLine.setBackgroundColor(getResources().getColor(R.color.c3F86FF));
                    if (listener != null) {
                        listener.onFocusChange(hasFocus);
                    }
                } else {
                    viewLine.setBackgroundColor(getResources().getColor(R.color.cDBDBDB));
                    if (listener != null) {
                        listener.onFocusChange(hasFocus);
                    }
                }
            }

            @Override
            public void afterTextChanged(String s) {
                if (TextUtils.isEmpty(s)) {
                    if (listener != null) {
                        listener.inputViewHasText(false);
                    }
                } else {
                    if (listener != null) {
                        listener.inputViewHasText(true);
                    }
                }

            }
        });
    }


    public String getText() {
        return etAccount.getText().toString().trim().replaceAll(" ", "");
    }

    public AccountInputView setHintText(String text) {
        etAccount.setHint(text);
        return this;
    }

    public AccountInputView setInputType(int type) {
        etAccount.setInputType(type);
        return this;
    }

    public AccountInputView setPhoneMode() {
        etAccount.setPhoneMode();
        etAccount.setFilters(new InputFilter[] { new InputFilter.LengthFilter(13) });
        return this;
    }

    public AccountInputView isHideUnderline(boolean isHide) {
        if (isHide) {
            viewLine.setVisibility(GONE);
        } else {
            viewLine.setVisibility(VISIBLE);
        }
        return this;
    }

    public AccountInputView setHintColor(int color) {
        etAccount.setHintTextColor(color);
        return this;
    }

    public AccountInputView setTextSize(float size) {
        etAccount.setTextSize(size);
        return this;
    }

    public AccountInputView setTextColor(int color) {
        etAccount.setTextColor(color);
        return this;
    }

    public AccountInputView setMaxLength(int maxLength) {
        etAccount.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxLength) });
        return this;
    }

    public AccountInputView setDrawableLeft(Drawable drawableLeft) {
        etAccount.setDrawableLeft(drawableLeft);
        return this;
    }

}
